package com.neuedu.helloworld;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;


/**
 * Main
 *
 * 支持 渲染Fragment  显示部分内容
 */
public class MainActivity extends AppCompatActivity {


    private TabLayout tabs ;
    private ViewPager viewPager;

    String[] tabNames = {"车票","订单","我的"};

    List<Fragment> fragments = new ArrayList<>();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // getIntent();

        // id

        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.viewPager);
        tabs = findViewById(R.id.tabs);


        fragments.add(new TicketFragment());
        fragments.add(new OrdertFragment());
        fragments.add(new MyFragment());



        tabs.setupWithViewPager(viewPager);
        tabs.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager){
            @Override
            public void onTabSelected(@NonNull TabLayout.Tab tab) {
                setTitle(tab.getText());
                super.onTabSelected(tab);
            }
        });



        //设置 适配器用于渲染  不同 的滑动页面
        viewPager.setAdapter(    new MyFragmentPagerAdapter(getSupportFragmentManager()));




        //设置了下 选项卡的内容
//        for (String tabName : tabNames) {
//            tabs.addTab(tabs.newTab().setText(tabName));
//        }

    }




    class MyFragmentPagerAdapter extends FragmentPagerAdapter {


        public MyFragmentPagerAdapter(@NonNull FragmentManager fm) {
            super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }


        //有几个
        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return tabNames[position];
        }
    }






}